//
//  ConfigXp.m
//  
//
//  Created by xp on 16/5/2.
//
//

#import "ConfigXp.h"

@implementation ConfigXp

UIColor* getMainColor(){
    return getColor(@"47c88a");
}

UIColor* getThreadColor(){
    return getColor(@"eeeeee");
}

+ (UILabel *)labelWithFrame:(CGRect)frame
                       text:(NSString *)text
                  textColor:(UIColor *)textColor
                       font:(UIFont *)font
              textAlignment:(NSTextAlignment)textAlignment
            backgroundColor:(UIColor *)bgColor{
    
    UILabel *Label = [[UILabel alloc]initWithFrame:frame];
    Label.text = text;
    Label.textColor = textColor;
    Label.font = font;
    Label.textAlignment = textAlignment;
    Label.backgroundColor = bgColor;
    return Label;
}

+ (UIButton *)buttonWithFrame:(CGRect)frame
                        title:(NSString *)title
                   titleColor:(UIColor *)titleColor
                         font:(UIFont *)font
              backgroundColor:(UIColor *)bgColor
                        image:(UIImage *)image{

    UIButton *button = [[UIButton alloc]init];
    button.frame = frame;
    button.titleLabel.font = font;
    button.backgroundColor = bgColor;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    [button setImage:image forState:UIControlStateNormal];
    return button;
}

NSString* timeStamp_time(NSString *timeStamp){
    
    NSTimeInterval time = [timeStamp doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *currentTime = [formatter stringFromDate:date];
    
    return currentTime;
}

NSString* timeStamp_date(NSString *timeStamp){
    
    NSTimeInterval time = [timeStamp doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSString *currentTime = [formatter stringFromDate:date];
    
    return currentTime;
}

NSString* getDate(){
    
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *DateTime = [formatter stringFromDate:date];
    
    return DateTime;
}

NSString* getTimeStamp(){
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; //hh与HH的区别:分别表示12小时制,24小时制
    //设置时区,这个对于时间的处理有时很重要
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate *date = [NSDate date];//现在时间
    //时间转时间戳的方法:
    NSString *timeStamp = [NSString stringWithFormat:@"%@",[NSNumber numberWithDouble:[date timeIntervalSince1970]]];
    
    return timeStamp;
}

CGFloat scrWidth(){
    return [UIScreen mainScreen].bounds.size.width;
}

CGFloat scrHeight(){
    return [UIScreen mainScreen].bounds.size.height;
}

CGFloat screenScale(CGFloat range){
    return range * [UIScreen mainScreen].bounds.size.width / 375.0;
}

NSString *getDeviceType(){
    NSString *typeString;
    if ( scrWidth() > 375 ) {
        typeString = (NSString *)kIphone6Plus;
    }
    else if ( scrWidth() > 320 ){
        typeString = (NSString *)kIphone6;
    }
    else if ( scrHeight() > 480 ){
        typeString = (NSString *)kIphone5;
    }
    else{
        typeString = (NSString *)kIphone4;
    }
    return typeString;
}

UIColor* getColor(NSString * hexColor)
{
    unsigned int red, green, blue;
    NSRange range;
    range.length = 2;
    range.location = 0;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];
    range.location = 2;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];
    range.location = 4;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];
    
    return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green/255.0f) blue:(float)(blue/255.0f) alpha:1.0f];
}

UIFont * get4sFont(NSInteger size){
    int isIphone6 = (int)(getDeviceType() == kIphone6);
    int isIphone6Plus = (int)(getDeviceType() == kIphone6Plus);
    NSInteger fontSize = size + isIphone6 * kIphone6FontScale + isIphone6Plus * kIphone6PlueFontScale;
    return [UIFont systemFontOfSize:fontSize];
}

NSString *getFileName(){
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = array[0];
    NSString *fileName = [path stringByAppendingPathComponent:@"data.plist"];
    return fileName;
}

NSComparisonResult compareDate(NSDate *date1,NSDate *date2){
    int secondsInDay = 24 * 60 * 60;
    NSInteger allDays1 = [date1 timeIntervalSince1970] / secondsInDay;
    NSInteger allDays2 = [date2 timeIntervalSince1970] / secondsInDay;
    NSComparisonResult result;
    if (allDays1 < allDays2) {
        result = NSOrderedAscending;
    }
    else if(allDays1 == allDays2){
        result = NSOrderedSame;
    }
    else if(allDays1 > allDays2){
        result = NSOrderedDescending;
    }
    return result;
}

BOOL getLoginState(){
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    return [[userDefault objectForKey:@"isLogin"] boolValue];
}

@end
